﻿using System;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace TouchControls
{
    [ContentPropertyAttribute("Content")]
    public class PivotItem : ContentControl
    {
        public event EventHandler OnItemActivated;

        private TranslateTransform _translate;
        
        private FrameworkElement _itemContent;
        internal FrameworkElement ItemContent
        {
            get
            {
                return _itemContent;
            }
        }

        public PivotItem()
            : base()
        {
            this.DefaultStyleKey = typeof(PivotItem);
        }

        internal void ActivateItem()
        {
            if (OnItemActivated != null) OnItemActivated(this, new EventArgs());
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _itemContent = (FrameworkElement)this.GetTemplateChild("PivotItemContent");
            _translate = (TranslateTransform)this.GetTemplateChild("ItemContentTranslate");
            _translateName = "TranslateContent" + _translate.GetHashCode();
            this.RegisterName(_translateName, _translate);
        }

        #region Title
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register(
                "Title",
                typeof(object),
                typeof(PivotItem),
                new PropertyMetadata(OnTitleChanged));
        private string _translateName;
        
        public object Title
        {
            get { return GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        private static void OnTitleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PivotItem ctrl = (PivotItem)d;
            ctrl.OnTitleChanged(e.OldValue, e.NewValue);
        }

        protected virtual void OnTitleChanged(object oldHeader, object newHeader)
        {
        }
        #endregion Title

        #region Header
        public PivotHeader Header
        {
            get;
            internal set;
        }
        #endregion Header

        internal void ShowFromRight()
        {
            ShowContent(ActualWidth);
        }

        internal void ShowFromLeft()
        {
            ShowContent(-ActualWidth);
        }

        private void ShowContent(double fromValue)
        {
            Storyboard stb = new Storyboard();
            var stbName = "ShowContentSTB" + stb.GetHashCode();
            Resources.Add(stbName, stb);
            
            var ease = new QuadraticEase();
            ease.EasingMode = EasingMode.EaseOut;

            stb.DoubleAnim(_translateName, "X", fromValue: fromValue, toValue: 0, duration: 300, easing: ease);            
            stb.DoubleAnim(this, "Opacity", fromValue: 0, toValue: 1, duration: 200, easing: ease);

            stb.Completed += (sender, args) =>
            {
                Resources.Remove(stbName);
                stb.Stop();
                stb.Children.Clear();
            };

            stb.Begin();
        }
    }
}
